﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace SMS.Client
{
    public partial class SearchClient : System.Web.UI.Page
    {
        SMS.Business.Customer.CustomerBussiness CustBuss;
        List<SMS.DAL.Customer> CustFound
        {
            set
            {
                ViewState["CustFound"] = value;
            }
            get
            {
                return (List<SMS.DAL.Customer>)ViewState["CustFound"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");
            CustBuss = new Business.Customer.CustomerBussiness();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            CustFound = CustBuss.Search(new DAL.Providers.CustomerSearchCriteria()
            {
                Name = txtName.Text,
                Status = rblStatus.SelectedIndex
            });
            GridView1.DataSource = CustFound;
            this.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("NewClient.aspx?ClientID=" + SMS.Helper.QueryEncoder.Encrypt(GridView1.SelectedDataKey.Value.ToString()));
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            CustBuss.Delete(int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()));
            CustFound.Remove(CustFound.Where(t => t.ID == int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString())).SingleOrDefault());
            GridView1.DataSource = CustFound;          
            this.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ViewTrans")
            {
                Response.Redirect("ClientTransactions.aspx?ClientID=" + GridView1.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString());
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                SMS.DAL.Customer c = (SMS.DAL.Customer)(e.Row.DataItem);
                if (c.Credit == 0)
                {
                    e.Row.Cells[3].Text = "0";
                    e.Row.Cells[4].Text = "0";
                }
                else
                {
                    if (c.Credit > 0)
                    {
                        e.Row.Cells[3].Text = c.Credit.ToString();
                        e.Row.Cells[3].BackColor = Color.LightGreen;
                        e.Row.Cells[4].Text = "0";
                    }
                    else
                    {
                        e.Row.Cells[3].Text = "0";
                        e.Row.Cells[4].Text = Math.Abs((decimal)c.Credit).ToString();
                        e.Row.Cells[4].BackColor = Color.FromArgb(248, 129, 88);

                    }
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = CustFound;
            this.DataBind();
        }
    }
}